﻿#include "ShowDiscInfo.h"
#include <iostream>
#include <qcoreapplication.h>
#include <qhash.h>
#include <StdClassFun.h>
#include <ThreadShowDiscsOnWin.h>

ShowDiscInfo::ShowDiscInfo(int argc
	, char* argv[]) {
	paramter = initParameters(argc, argv, exeName);
	ThreadShowDiscsOnWin* discsOnWin = new ThreadShowDiscsOnWin( );
	connect(discsOnWin, &ThreadShowDiscsOnWin::sendDiscInfo, this, &ShowDiscInfo::sendDiscInfo);
	discsOnWin->start( );
}

void ShowDiscInfo::sendDiscInfo(QHash<QString, QHash<QString, ULARGE_INTEGER>>* discInfo) {
	if( !discInfo->isEmpty( ) ) {
		auto iterator = discInfo->begin( );
		auto end = discInfo->end( );
		for( ;iterator != end;++iterator ) {
			// 获取可用空间的数据
			auto sfreeBytesAvailable = iterator.value( ).value(getFreeBytesAvailableKey( ));
			qreal gbFreeBytesAvailable = getGb(sfreeBytesAvailable.HighPart, sfreeBytesAvailable.LowPart);
			//  获取总空间数据
			auto stotalNumberOfBytesKey = iterator.value( ).value(getTotalNumberOfBytesKey( ));
			qreal gbTotalNumberOfBytesKey = getGb(stotalNumberOfBytesKey.HighPart, stotalNumberOfBytesKey.LowPart);
			// 获取剩余空间数据
			auto stotalNumberOfFreeBytes = iterator.value( ).value(getTotalNumberOfFreeBytes( ));
			qreal gbTotalNumberOfFreeBytes = getGb(stotalNumberOfFreeBytes.HighPart, stotalNumberOfFreeBytes.LowPart);
			QString chars;
			chars = chars.sprintf("\n盘符\t\t%s\n总空间\t\t%8.2f\tG\n可用数据\t%8.2f\tG\n剩余空间\t%8.2f\tG", iterator.key( ).toStdString( ).c_str( ), gbFreeBytesAvailable, gbTotalNumberOfBytesKey, gbTotalNumberOfFreeBytes);
			//QString buff("\n=====================\n盘符\t\t" + iterator.key( ) + "\n可用数据\t" + chars.arg(gbFreeBytesAvailable) + "\t\n总空间\t\t" + chars.arg(gbTotalNumberOfBytesKey) + "\t\n剩余空间\t" + chars.arg(gbTotalNumberOfFreeBytes));
			std::cout << chars.toLocal8Bit( ).constData( ) << std::endl;
		}
	}

	// 释放

	QObject* object = sender( );
	delete object;
	qApp->quit( );
}
